package com.mm.controller;

import com.mm.service.CategoryInfoService;
import com.mm.bo.CategoryInsertBo;
import com.mm.bo.CategorySearchBo;
import com.mm.bo.CategoryUpdateBo;
import com.mm.exception.CustomException;
import com.mm.util.GetHeadUtil;
import com.mm.util.PageUtil;
import com.mm.util.ResultUtil;
import com.mm.vo.CategoryInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @ClassName CategoryInfoController
 * @Description TODO
 * @Author 任昊
 * @Date 2020/12/7 10:11
 */
@Api(tags = "用于物资品类管理的接口")
@RestController
public class CategoryInfoController {

    @Resource
    private CategoryInfoService categoryInfoService;


    @ApiOperation(value = "新增物资种类", notes = "新增物资种类")
    @PostMapping("/v1/goods/insertCategory")
    public ResultUtil insertCategory(@RequestBody @Valid CategoryInsertBo categoryInsertBo) {
        categoryInsertBo.setToken(GetHeadUtil.getHeadToken());
        Integer row = categoryInfoService.insertCategoryInfo(categoryInsertBo);
        if (row < 1) {
            throw new CustomException("新增失败");
        }
        return ResultUtil.success(null);
    }

    @ApiOperation(value = "删除物资种类", notes = "删除物资种类")
    @GetMapping("/v1/goods/deleteCategory/{cateId}")
    @ApiImplicitParam(value = "种类id(大于1个传数组 [  ])", name = "cateId", required = true)
    public ResultUtil deleteCategory(@PathVariable("cateId") String cateId) {
        Integer row = categoryInfoService.deleteCategoryInfo(cateId, GetHeadUtil.getHeadToken());
        if (row < 1) {
            throw new CustomException("删除失败");
        }
        return ResultUtil.success(null);
    }

    @ApiOperation(value = "修改物资种类", notes = "修改物资种类")
    @PostMapping("/v1/goods/updateCategory")
    public ResultUtil updateCategory(@RequestBody @Valid CategoryUpdateBo categoryUpdateBo) {
        categoryUpdateBo.setToken(GetHeadUtil.getHeadToken());
        Integer row = categoryInfoService.UpdateCategoryInfo(categoryUpdateBo);
        if (row < 1) {
            throw new CustomException("修改失败");
        }
        return ResultUtil.success(null);
    }

    @ApiOperation(value = "查询物资种类", notes = "查询物资种类")
    @PostMapping("/v1/goods/selectCategory")
    public ResultUtil<Object> selectCategory(@RequestBody @Valid CategorySearchBo categorySearchBo) {
        PageUtil<CategoryInfoVo> data = categoryInfoService.selectCategoryInfo(categorySearchBo);
        return ResultUtil.success(data);
    }

}
